import pygame
import sys


def esc():
    pygame.quit()
    sys.exit()


pygame.init()

winx = 300
winy = 300
screen = pygame.display.set_mode((winx, winy))
screen.fill((255, 255, 255))
B = 0, 0, 0
x = 100
y = 100
speed = 5
speedx = speed
speedy = speed
fclock = pygame.time.Clock()
direct = 'right'
while True:
    for event in pygame.event.get():
        # print(event.type)
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_ESCAPE:
                esc()
            elif event.key == pygame.K_LEFT:
                print("left")
                direct = 'left'
            elif event.key == pygame.K_RIGHT:
                print("right")
                direct = 'right'
            elif event.key == pygame.K_UP:
                print("up")
            elif event.key == pygame.K_DOWN:
                print("down")

        if event.type == pygame.QUIT:
            esc(event)
    rectT = (x, y, 50, 50)
    if direct == 'right':
        speedx = speed
    elif direct == 'left':
        speedx = -speed

    x += speedx
    # y += speedy
    # print(x)
    if x > winx:
        x = 0
    elif x + 50 < 0:
        x = winx

    r1 = pygame.Rect(rectT)
    r = pygame.Rect(100, 100, 50, 50)
    # print('r.x, r.y: ', r1.x,r1.y,r.width,r.height)
    print("collide:", pygame.Rect.colliderect(r1, r))
    screen.fill((255, 255, 255))
    pygame.draw.rect(screen, (0, 0, 0), rectT, 0)
    pygame.draw.rect(screen, (0, 0, 0), r, 0)
    pygame.display.update()
    fclock.tick(5)
